
package com.ido.babyshow.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ido.babyshow.common.GlobalConstant;
import com.ido.babyshow.model.Catalog;
import com.ido.babyshow.model.Mother;
import com.ido.babyshow.model.Photo;
import com.ido.babyshow.model.SecCatalog;
import com.ido.babyshow.model.Catalog.CataComparetor;
import com.ido.babyshow.response.BaseResponse;
import com.ido.babyshow.service.IMotherService;
import com.ido.babyshow.service.impl.CatalogService;
import com.ido.babyshow.service.impl.SecCatalogService;
import com.ido.babyshow.util.JsonUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 首页action处理
 * 
 * @author luo sq
 */
@Component
@Scope("prototype")
public class LoginAction extends ActionSupport
{
    private final static Logger logger =
        LoggerFactory.getLogger(LoginAction.class);

    @Resource
    private CatalogService catalogService;

    @Resource
    private SecCatalogService secCatalogService;

    @Resource
    private IMotherService motherService;

    private Map<Catalog, List<SecCatalog>> catalogMap =
        new TreeMap<Catalog, List<SecCatalog>>(new CataComparetor());

    /** 所有栏目信息 **/
    // private List<Catalog> catalogList;

    private List<SecCatalog> secCatalogList;

    private Mother mother;

    /** model大图 **/
    private Photo modelPhoto;

    public Map<Catalog, List<SecCatalog>> getCatalogMap()
    {
        return catalogMap;
    }

    public List<SecCatalog> getSecCatalogList()
    {
        return secCatalogList;
    }

    public Photo getModelPhoto()
    {
        return modelPhoto;
    }

    public void setCatalogService(CatalogService catalogService)
    {
        this.catalogService = catalogService;
    }

    public Mother getMother()
    {
        return mother;
    }

    public void setMother(Mother mother)
    {
        this.mother = mother;
    }

    public void validateEmail()
    {
        try
        {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            String email = request.getParameter("email");
            BaseResponse result = new BaseResponse();
            result.setFieldId("email");
            if (StringUtils.isBlank(email))
            {
                result.setMessage("邮箱不能为空!");
            }
            else
            {
                boolean emailExist = motherService.validateEmail(email);
                if (emailExist)
                {
                    result.setMessage("邮箱已经被注册,请使用另外的邮箱!");
                }
                else
                {

                    result.setFlag(true);
                    result.setMessage("邮箱可以使用!");
                }
            }
            PrintWriter out = response.getWriter();
            JsonUtil.writeJson(out, result);
        }
        catch (Exception e)
        {
            logger.error("验证用户邮箱异常", e);
            e.printStackTrace();
        }

    }

    public String forwardRegister()
    {
        return SUCCESS;
    }

    public String register()
    {

        if (mother != null)
        {
            motherService.registerMother(mother);
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpSession session = request.getSession(true);
            mother = motherService.selectByEmail(mother.getEmail());
            session.setAttribute(GlobalConstant.MOTHER_SESSION, mother);
        }

        return SUCCESS;
    }

    public void login()
    {
        try
        {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            BaseResponse result = new BaseResponse();
            String email = request.getParameter("email");
            String password = request.getParameter("password");
            logger.debug("用户登录,email:"+email+",password:"+password);
            if (StringUtils.isBlank(email))
            {
                result.setMessage("邮箱不能为空!");
            }
            else if (StringUtils.isBlank(password))
            {
                result.setMessage("密码不能为空!");
            }
            else
            {
                Mother mother = motherService.login(email, password);
                if (mother == null)
                {
                    result.setMessage("用户名或者密码错误!");
                }
                else
                {
                    result.setFlag(true);
                    HttpSession session = request.getSession(true);
                    session.setAttribute(GlobalConstant.MOTHER_SESSION, mother);
                }
            }
            PrintWriter out = response.getWriter();
            JsonUtil.writeJson(out, result);
        }
        catch (Exception ex)
        {
            logger.error("用户登录异常", ex);
            ex.printStackTrace();
        }

    }
}
